#require 'bundler/capistrano'

set :application, "tressaweb"
set :repository,  "https://tressaweb.googlecode.com/svn/trunk/tressaweb"

set :scm, :subversion

#############################################################
#	Settings
#############################################################

default_run_options[:pty] = true
ssh_options[:forward_agent] = true
ssh_options[:keys] = ["/home/brian/tressakey.pem"]
set :use_sudo, false
set :scm_verbose, true
set :group_writable, false

#############################################################
#	Application
#############################################################

#DEV
#set :user, "brianroi"
#PROD
set :user, "ec2-user"

set :rails_env, "production"

# The domain name of the server to deploy to, this can be your domain or the domain of the server.
#DEV
#set :server_name, "brianroisentul.com.ar"
#PROD
set :server_name, "52.7.254.138"

#DEV
#set :deploy_to, "/home/#{user}/#{application}"
#PROD
set :deploy_to, "/var/www/#{application}"

role :app, server_name
role :web, server_name
role :db, server_name, :primary => true

# if you want to clean up old releases on each deploy uncomment this:
# after "deploy:restart", "deploy:cleanup"

# if you're still using the script/reaper helper you will need
# these http://github.com/rails/irs_process_scripts
namespace :deploy do
  task :after_update_code, :roles => [:web, :db, :app] do
    run "chmod 755 #{release_path}/public"
    run "chmod 755 #{release_path}/public/dispatch.fcgi"
    run "chmod 755 #{release_path}/public/dispatch.cgi"
    run "chmod 755 -R #{current_path}"

  end

  namespace :deploy do
    desc "cold deploy"
    task :cold do
      update
      fastcgi::restart
      less::generate
    end

    desc "restart dispatchers"
    task :restart do
      passenger::restart
      symlinks_creation::create_symlinks
    end
  end

  namespace :bundle do
    desc 'Runs bundle install command'
    task :run_bundle do
      run("sh -c 'cd #{deploy_to}/current && /usr/bin/env bundle install --path vendor/bundle RAILS_ENV=production'")
    end
  end

#  namespace :deploy do
#    desc "Create asset packages for production"
#    task :after_update_code, :roles => [:web] do
#      run <<-EOF
#        cd #{release_path} && bundle exec rake asset:packager:build_all RAILS_ENV=production
#      EOF
#    end
#  end

  namespace :passenger do
    desc 'Restart Passenger'
    task :restart do
      run "touch #{current_path}/tmp/restart.txt"
    end
  end

  namespace :fastcgi do
    desc 'Restart Dispatchers'
    task :restart do
      run "ps -U #{user} | grep dispatch.fcgi | awk '{print $1}' | xargs kill -9"
    end
  end

  namespace :symlinks_creation do
    desc 'Creates the symlinks needed'
    task :create_symlinks do
      run "ln -s #{shared_path}/photos #{release_path}/public/system/photos"
    end
  end

  # Restart passenger on deploy
  desc "Restarting mod_rails with restart.txt"
  task :restart, :roles => :app, :except => {:no_release => true} do
    run "touch #{current_path}/tmp/restart.txt"
  end


  [:start, :stop].each do |t|
    desc "#{t} task is a no-op with mod_rails"
    task t, :roles => :app do
      ;
    end
  end
end